﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using GC2.Bussines;
using GC2.Bussines.BMAdmin;
using GC2.Entities.Admin;
using GC2.Entities.Seguridad;
using GC2.ExceptionHandler;

public partial class Admin_ConceptosAdmin : GenericPage
{
    Usuarios usuario = new Usuarios();
    Conceptos conceptoActual = new Conceptos();
    List<Conceptos> listaConceptos = new List<Conceptos>();
    private ExceptionConstructor exNew = new ExceptionConstructor();

    #region Controles

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            SetearCultura();
            SetearConexion();
            if (!IsPostBack)
            {
                usuario = (Usuarios)Session["UsuarioActual"];
                rdbEgresos.Checked = true;
            }
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void rdbEgresos_CheckedChanged(object sender, EventArgs e)
    {
        try
        {
            if (rdbEgresos.Checked)
            {
                rdbIngresos.Checked = false;
                rdbTodos.Checked = false;
                BuscarConceptos();
            }
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void rdbIngresos_CheckedChanged(object sender, EventArgs e)
    {
        try
        {
            if (rdbIngresos.Checked)
            {
                rdbEgresos.Checked = false;
                rdbTodos.Checked = false;
                BuscarConceptos();
            }
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void rdbTodos_CheckedChanged(object sender, EventArgs e)
    {
        try
        {
            if (rdbTodos.Checked)
            {
                rdbEgresos.Checked = false;
                rdbIngresos.Checked = false;
                BuscarConceptos();
            }
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void btnBuscar_Click(object sender, EventArgs e)
    {
        try
        {
            BuscarConceptos();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void btnNuevo_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtCodigo.Text.Trim() != "")
            {
                conceptoActual.Id_Concepto = Convert.ToInt16(txtCodigo.Text.Trim());
            }
            else
            {
                conceptoActual.Id_Concepto = 0;
            }

            conceptoActual.Id_Tipo = 0;
            if (rdbEgresos.Checked)
            {
                conceptoActual.Id_Tipo = 1;
            }
            if (rdbIngresos.Checked)
            {
                conceptoActual.Id_Tipo = 2;
            }
            if (conceptoActual.Id_Tipo > 0)
            {
                conceptoActual.Concepto = txtConcepto.Text.Trim();
                Session["Accion"] = 1;
                Session["Concepto"] = conceptoActual;
                Response.Redirect("ConceptosGestion.aspx", false);
            }
            else
            {
                // Mensaje: debe seleccionar Egreso o Ingreso
                mpeTipo.Show();
            }
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void btnCancelar_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("~/Seguridad/Default.aspx", false);
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void gvConceptos_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            gvConceptos.PageIndex = e.NewPageIndex;
            CargarGrilla();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void gvConceptos_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try { }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void gvConceptos_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            BMConceptos bmConceptos = new BMConceptos(Connection);
            Conceptos concepto = new Conceptos();
            concepto.Id_Concepto = (int)gvConceptos.SelectedDataKey.Value;
            conceptoActual = concepto;
            listaConceptos = bmConceptos.BMConceptosSearch(conceptoActual);
            concepto = listaConceptos[0];

            Session["Concepto"] = concepto;
            Session["Accion"] = 2;
            Response.Redirect("ConceptosGestion.aspx", false);
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    #endregion

    #region Métodos
    private void CargarGrilla()
    {
        try
        {
            listaConceptos = (List<Conceptos>)Session["listaConceptos"];
            gvConceptos.DataSource = listaConceptos;
            gvConceptos.DataBind();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    private void BuscarConceptos()
    {
        try
        {
            BMConceptos bmConceptos = new BMConceptos(Connection);

            if (txtCodigo.Text.Trim() != "")
            {
                conceptoActual.Id_Concepto = Convert.ToInt16(txtCodigo.Text.Trim());
            }
            else
            {
                conceptoActual.Id_Concepto = 0;
            }

            conceptoActual.Id_Tipo = 0;
            if (rdbEgresos.Checked)
            {
                conceptoActual.Id_Tipo = 1;
            }
            if (rdbIngresos.Checked)
            {
                conceptoActual.Id_Tipo = 2;
            }

            if (txtCodEx.Text.Trim() != "")
            {
                conceptoActual.CodEx = txtCodEx.Text.Trim();
            }

            conceptoActual.Concepto = txtConcepto.Text.Trim();

            listaConceptos = bmConceptos.BMConceptosSearch(conceptoActual);

            Session["listaConceptos"] = listaConceptos;
            CargarGrilla();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    #endregion

  
}
